﻿#include "stdafx.h"
#include "qg/qgasv.h"

//////////////////////////////////////////////////////////////////////////
// 버퍼
KIMPL_SUBCLASS_ABSTRACT(qgBuffer, "AbstractDrawBuffer");

//
bool qgBuffer::SetIndices(kconstpointer data, kint count /*= 0*/, bool is32bit /*= true*/)
{
	k_return_value_if_fail(data, false);
	k_return_value_if_fail(_buftype == QGBUFFER_INDEX, false);

	if (count <= 0)
		count = _count;

	kpointer lock = EnterMap();
	k_return_value_if_fail(lock, false);

	if (_stride == 4)
	{
		if (is32bit)
			memcpy(lock, data, count*sizeof(kint));
		else
		{
			kushort* b16 = (kushort*)data;
			kuint* b32 = (kuint*)lock;

			for (kint i = 0; i < count; i++)
			{
				*b32 = *b16;
				b16++;
				b32++;
			}
		}
	}
	else
	{
		if (!is32bit)
			memcpy(lock, data, count*sizeof(kushort));
		else
		{
			kushort* b16 = (kushort*)lock;
			kuint* b32 = (kuint*)data;

			for (kint i = 0; i < count; i++)
			{
				*b16 = (kushort)*b32;
				b16++;
				b32++;
			}
		}
	}

	LeaveMap();

	return true;
}

//
bool qgBuffer::SetVertices(kconstpointer data, kint count /*= 0*/)
{
	k_return_value_if_fail(data, false);
	k_return_value_if_fail(_buftype == QGBUFFER_VERTEX, false);

	if (count <= 0)
		count = _count;

	kpointer lock = EnterMap();
	k_return_value_if_fail(lock, false);

	memcpy(lock, data, _stride*count);

	LeaveMap();

	return true;
}

